<?php
namespace app\work\controller;
use app\work\logic\ActiveLogic;

/**
 * 活动开展api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/30
 * Time: 15:31
 */
class Active extends Base {

    public function __construct(){
        parent::__construct();
        $this->a_logic = new ActiveLogic();
    }

    /**
     * 新增活动开展
     * @return \think\response\Json
     */
    public function ajax_add_active(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->a_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增活动开展规则
        $r = $this->a_logic->check_add_active_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wra_data = [];
        $wra_data['openid'] = $u_detail['openid'];
        $wra_data['name'] = trim($params['name']);
        $wra_data['content'] = trim($params['content']);
        $wra_data['person_num'] = trim($params['person_num']);
        $wra_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
        $wraid = model('work_record_active')->insertGetId($wra_data);
        if($wraid){
            //同步发送工作圈
            $r = $this->a_logic->sync_send_wcm($u_detail['openid'],$params);

            //更新发布活动开展积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_ra_integral($wraid);

            //生成抄送
            $c = new \app\common\controller\Copy();
            if(!empty($params['copy_openid'])){
                //生成抄送（未抄送）
                $r = $c->gen_copy_not($u_detail['openid'],$wraid,5,$params['copy_openid']);
                //修改抄送（已抄送）
                $r = $c->save_copy_open($u_detail['openid'],$wraid,5);
            }

            return json($this->a_logic->get_response(1,'提交成功'));
        }
        return json($this->a_logic->get_response(1,'提交失败'));
    }
}
